/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function (root) {
  if (!root) return true;

  function checkSymmetry(leftNode, rightNode) {
    // 两个节点都为空，对称
    if (!leftNode && !rightNode) return true;

    // 只有一个节点为空，不对称
    if (!leftNode || !rightNode) return false;

    // 节点值不相等，不对称
    if (leftNode.val !== rightNode.val) return false;

    // 递归检查子树：左子树的左节点和右子树的右节点对称，左子树的右节点和右子树的左节点对称
    return (
      checkSymmetry(leftNode.left, rightNode.right) &&
      checkSymmetry(leftNode.right, rightNode.left)
    );
  }

  return checkSymmetry(root.left, root.right);
};